#include "ProgramManager.h"

ProgramManager::ProgramManager( QWidget* parent )
	: QSplitter( Qt::Vertical, parent )
{
		//Manager
		QWidget* managerWidget = new QWidget( this );
		QVBoxLayout* layout = new QVBoxLayout( managerWidget );
		addWidget( managerWidget );

		// Buttons
		QHBoxLayout* buttonLayout = new QHBoxLayout();
		layout->addItem( buttonLayout );

		QPushButton* addButton = new QPushButton( tr( "Add" ), managerWidget );
		connect( addButton, SIGNAL( pressed() ), this, SLOT( add() ) );
		buttonLayout->addWidget( addButton );

		QPushButton* removeButton = new QPushButton( tr( "Remove" ), managerWidget );
		connect( removeButton, SIGNAL( pressed() ), this, SLOT( remove() ) );
		buttonLayout->addWidget( removeButton );

		mTableView = new QTableView( this );
		connect( mTableView, SIGNAL( clicked( const QModelIndex& ) ), this, SLOT( selected( const QModelIndex& ) ) );
		mTableView->setModel( Database::instance()->programModel() );
		addWidget( mTableView );

		mScheduler = new Scheduler( this, Database::DT_PROGRAM );
		addWidget( mScheduler );
}

ProgramManager::~ProgramManager()
{
}

void
ProgramManager::add()
{
	bool ok;
	int id, semester;
	
	id = QInputDialog::getInt( this, tr( "Id" ), tr( "Program Id:" ), 0, 0, 999999999, 1, &ok );
	if( ok )
	{
		semester = QInputDialog::getInt( this, tr( "Semester" ), tr( "Program Semester:" ), 0, 0, 999999999, 1, &ok );
	}
	if( ok )
	{
		QString name = QInputDialog::getText( this, tr( "Disipline" ), tr( "Disipline:" ), QLineEdit::Normal, tr( "" ), &ok );
		if( ok && !name.isEmpty() )
		{
			Database::instance()->query( tr( "insert into " ) + 
				                         PROGRAM + 
										 tr( " values(" ) + 
										 QString::number( id ) + 
										 tr( ", " ) +
										 QString::number( semester ) + 
										 tr( ", '" ) +
										 name +
										 tr( "' )" ) );
		}
	}
}

void
ProgramManager::remove()
{
}

void
ProgramManager::selected( const QModelIndex& index )
{
	if( index.column() == 0 )
	{
		mScheduler->loadSchedule( index.data().toInt() );
	}
}